using System;
using System.Collections.Generic;
using System.Text;
using Fireball.Docking;
using Fireball.CodeEditor;
using GLuaR.Classes.Workspace;

using System.Windows.Forms;

namespace GLuaR.Windows.DockingWindows
{
    class CodeTab : DockableWindow
    {
        public Fireball.Windows.Forms.CodeEditorControl editor;
        private System.ComponentModel.IContainer components;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem findSelectedInGmodWikiToolStripMenuItem;
        private ToolStripMenuItem copyToolStripMenuItem1;
        private ToolStripMenuItem pasteToolStripMenuItem1;
        private ToolStripMenuItem cutToolStripMenuItem1;
        private ToolStripMenuItem pasteAndCutToolStripMenuItem;
        private ToolStripSeparator toolStripSeparator1;
        public Fireball.Syntax.SyntaxDocument document;

    public CodeTab(OpenedFile file)
    {
        InitializeComponent();

        switch( GetFileType( file.Name ) )
        {
            case "txt":
                Fireball.CodeEditor.SyntaxFiles.CodeEditorSyntaxLoader.SetSyntax(editor, Fireball.CodeEditor.SyntaxFiles.SyntaxLanguage.Text);
                break;

            case "lua":
            default:
                Fireball.CodeEditor.SyntaxFiles.CodeEditorSyntaxLoader.SetSyntax(editor, Fireball.CodeEditor.SyntaxFiles.SyntaxLanguage.Lua);
                break;
        }
        editor.Open(file.FullName);
        Tag = file;

    }

    private string GetFileType(string filename)
    {
       return filename.Substring(filename.Length - 3, 3).ToLower();
    }

        public void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            Fireball.Windows.Forms.LineMarginRender lineMarginRender1 = new Fireball.Windows.Forms.LineMarginRender();
            this.editor = new Fireball.Windows.Forms.CodeEditorControl();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.findSelectedInGmodWikiToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.copyToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.pasteToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.cutToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.pasteAndCutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.document = new Fireball.Syntax.SyntaxDocument(this.components);
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // editor
            // 
            this.editor.ActiveView = Fireball.Windows.Forms.CodeEditor.ActiveView.BottomRight;
            this.editor.AllowBreakPoints = false;
            this.editor.AutoListPosition = null;
            this.editor.AutoListSelectedText = "a123";
            this.editor.AutoListVisible = false;
            this.editor.ChildBorderStyle = Fireball.Windows.Forms.ControlBorderStyle.None;
            this.editor.ContextMenuStrip = this.contextMenuStrip1;
            this.editor.CopyAsRTF = false;
            this.editor.Dock = System.Windows.Forms.DockStyle.Fill;
            this.editor.Document = this.document;
            this.editor.Font = new System.Drawing.Font("Courier New", 10F);
            this.editor.InfoTipCount = 1;
            this.editor.InfoTipPosition = null;
            this.editor.InfoTipSelectedIndex = 1;
            this.editor.InfoTipVisible = false;
            lineMarginRender1.Bounds = new System.Drawing.Rectangle(19, 0, 19, 16);
            this.editor.LineMarginRender = lineMarginRender1;
            this.editor.Location = new System.Drawing.Point(0, 0);
            this.editor.LockCursorUpdate = false;
            this.editor.Name = "editor";
            this.editor.ParseOnPaste = true;
            this.editor.Saved = false;
            this.editor.ShowScopeIndicator = false;
            this.editor.ShowTabGuides = true;
            this.editor.Size = new System.Drawing.Size(680, 383);
            this.editor.SmoothScroll = true;
            this.editor.SplitView = false;
            this.editor.SplitviewH = -4;
            this.editor.SplitviewV = -4;
            this.editor.TabGuideColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(243)))), ((int)(((byte)(234)))));
            this.editor.TabIndex = 0;
            this.editor.TextDrawStyle = Fireball.Windows.Forms.CodeEditor.TextDraw.TextDrawType.SingleBorder;
            this.editor.VirtualWhitespace = true;
            this.editor.WhitespaceColor = System.Drawing.SystemColors.ControlDark;
            this.editor.TextChanged += new System.EventHandler(this.editor_TextChanged);
            this.editor.Click += new System.EventHandler(this.editor_Click);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.findSelectedInGmodWikiToolStripMenuItem,
            this.toolStripSeparator1,
            this.copyToolStripMenuItem1,
            this.pasteToolStripMenuItem1,
            this.cutToolStripMenuItem1,
            this.pasteAndCutToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(220, 120);
            // 
            // findSelectedInGmodWikiToolStripMenuItem
            // 
            this.findSelectedInGmodWikiToolStripMenuItem.Name = "findSelectedInGmodWikiToolStripMenuItem";
            this.findSelectedInGmodWikiToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F1;
            this.findSelectedInGmodWikiToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
            this.findSelectedInGmodWikiToolStripMenuItem.Text = "Search in gmod wiki";
            this.findSelectedInGmodWikiToolStripMenuItem.Click += new System.EventHandler(this.findSelectedInGmodWikiToolStripMenuItem_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(216, 6);
            // 
            // copyToolStripMenuItem1
            // 
            this.copyToolStripMenuItem1.Name = "copyToolStripMenuItem1";
            this.copyToolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C)));
            this.copyToolStripMenuItem1.Size = new System.Drawing.Size(219, 22);
            this.copyToolStripMenuItem1.Text = "Copy";
            this.copyToolStripMenuItem1.Click += new System.EventHandler(this.copyToolStripMenuItem1_Click);
            // 
            // pasteToolStripMenuItem1
            // 
            this.pasteToolStripMenuItem1.Name = "pasteToolStripMenuItem1";
            this.pasteToolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V)));
            this.pasteToolStripMenuItem1.Size = new System.Drawing.Size(219, 22);
            this.pasteToolStripMenuItem1.Text = "Paste";
            this.pasteToolStripMenuItem1.Click += new System.EventHandler(this.pasteToolStripMenuItem1_Click);
            // 
            // cutToolStripMenuItem1
            // 
            this.cutToolStripMenuItem1.Name = "cutToolStripMenuItem1";
            this.cutToolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.X)));
            this.cutToolStripMenuItem1.Size = new System.Drawing.Size(219, 22);
            this.cutToolStripMenuItem1.Text = "Cut";
            this.cutToolStripMenuItem1.Click += new System.EventHandler(this.cutToolStripMenuItem1_Click);
            // 
            // pasteAndCutToolStripMenuItem
            // 
            this.pasteAndCutToolStripMenuItem.Name = "pasteAndCutToolStripMenuItem";
            this.pasteAndCutToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift)
                        | System.Windows.Forms.Keys.X)));
            this.pasteAndCutToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
            this.pasteAndCutToolStripMenuItem.Text = "Paste and cut";
            this.pasteAndCutToolStripMenuItem.Click += new System.EventHandler(this.pasteAndCutToolStripMenuItem_Click);
            // 
            // document
            // 
            this.document.Lines = new string[] {
        ""};
            this.document.MaxUndoBufferSize = 1000;
            this.document.Modified = false;
            this.document.UndoStep = 0;
            // 
            // CodeTab
            // 
            this.ClientSize = new System.Drawing.Size(680, 383);
            this.Controls.Add(this.editor);
            this.DockableAreas = Fireball.Docking.DockAreas.Document;
            this.DoubleBuffered = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "CodeTab";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.TabText = "[ Document ]";
            this.Text = "[ Document ]";
            this.Load += new System.EventHandler(this.CodeTab_Load);
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        private void CodeTab_Load(object sender, EventArgs e)
        {
            
        }
        private void editor_TextChanged(object sender, EventArgs e)
        {

            this.TabText = this.editor.FileName + "*";
            

            /*if (this.Tag.GetType().Name == "OpenedFile")
            {
                this.TabText = "SHIT CHANGED";
           
                if (((OpenedFile)this.Tag).Saved == true)
                    this.TabText = this.TabText + "*";
                ((OpenedFile)this.Tag).Saved = false;
            }*/
        }

        private void editor_Click(object sender, EventArgs e)
        {
            
        }

        private void goToMainPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Workspace workspace = Program.MainWindow.workspace;
            workspace.ShowWiki("");
        }

        private void findSelectedInGmodWikiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Workspace workspace = Program.MainWindow.workspace;
            if (workspace.IsActiveTabSpecial())
                return;
            else
            {
                if (workspace.IsCodeTab(((Fireball.Docking.DockableWindow)workspace.manager.ActiveDocument)))
                {
                    Fireball.Windows.Forms.CodeEditorControl edit = ((CodeTab)workspace.manager.ActiveDocument).editor;
                    workspace.ShowWiki(edit.Selection.Text);
                }
            }
        }

        private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Workspace workspace = Program.MainWindow.workspace;
            if (workspace.IsActiveTabSpecial())
                return;
            else
            {
                if (workspace.IsCodeTab(((Fireball.Docking.DockableWindow)workspace.manager.ActiveDocument)))
                {
                    Fireball.Windows.Forms.CodeEditorControl edit = ((CodeTab)workspace.manager.ActiveDocument).editor;
                    if (edit.CanCopy)
                        edit.Copy();
                }
            }
        }

        private void pasteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Workspace workspace = Program.MainWindow.workspace;
            if (workspace.IsActiveTabSpecial())
                return;
            else
            {
                if (workspace.IsCodeTab(((Fireball.Docking.DockableWindow)workspace.manager.ActiveDocument)))
                {
                    Fireball.Windows.Forms.CodeEditorControl edit = ((CodeTab)workspace.manager.ActiveDocument).editor;
                    if (edit.CanPaste)
                        edit.Paste();
                }
            }
        }

        private void cutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Workspace workspace = Program.MainWindow.workspace;
            if (workspace.IsActiveTabSpecial())
                return;
            else
            {
                if (workspace.IsCodeTab(((Fireball.Docking.DockableWindow)workspace.manager.ActiveDocument)))
                {
                    Fireball.Windows.Forms.CodeEditorControl edit = ((CodeTab)workspace.manager.ActiveDocument).editor;
                    if (edit.CanCopy)
                        edit.Cut();
                }
            }
        }

        private void pasteAndCutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Workspace workspace = Program.MainWindow.workspace;
            string clp;
            if (workspace.IsActiveTabSpecial())
                return;
            else
            {
                if (workspace.IsCodeTab(((Fireball.Docking.DockableWindow)workspace.manager.ActiveDocument)))
                {
                    Fireball.Windows.Forms.CodeEditorControl edit = ((CodeTab)workspace.manager.ActiveDocument).editor;
                    if (edit.CanCopy && edit.CanPaste)
                    {
                        clp = edit.Selection.Text;
                        edit.Paste();
                        Clipboard.SetText(clp);
                    }
                }
            }
        }

    }
}
